package com.edu.exam.entity.curriculum.classification;

import com.edu.exam.enums.academic.CognitiveLevelCategory;
import com.edu.exam.enums.core.EntityStatus;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 认知层次实体类
 * 所属模块：F-07 基础数据管理模块
 * 对应数据库表：cognitive_levels
 *
 * 管理基于布鲁姆认知分类法的层次标准，包含认知层次的定义、层级关系、行为示例。
 * 支持认知层次的比较和定义查询。
 *
 * @author System
 * @version 1.0.0
 */
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Entity
@Table(name = "cognitive_levels")
@Comment("认知层次表")
public class CognitiveLevel {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    /**
     * 认知层次代码，唯一标识符
     */
    @NotBlank(message = "认知层次代码不能为空")
    @Size(max = 20, message = "认知层次代码长度不能超过20个字符")
    @Column(name = "level_code", nullable = false, unique = true, length = 20)
    private String levelCode;

    /**
     * 认知层次名称
     */
    @NotBlank(message = "认知层次名称不能为空")
    @Size(max = 50, message = "认知层次名称长度不能超过50个字符")
    @Column(name = "level_name", nullable = false, length = 50)
    private String levelName;

    /**
     * 分类
     */
    @NotNull(message = "分类不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "category", nullable = false, length = 20)
    private CognitiveLevelCategory category;

    /**
     * 层级序号
     */
    @NotNull(message = "层级序号不能为空")
    @Column(name = "hierarchy_level", nullable = false)
    private Integer hierarchyLevel;

    /**
     * 描述信息
     */
    @Size(max = 1000, message = "描述信息长度不能超过1000个字符")
    @Column(name = "description", columnDefinition = "TEXT")
    private String description;

    /**
     * 行为示例
     */
    @Size(max = 1000, message = "行为示例长度不能超过1000个字符")
    @Column(name = "example_behaviors", columnDefinition = "TEXT")
    private String exampleBehaviors;

    /**
     * 关键动词
     */
    @Size(max = 1000, message = "关键动词长度不能超过1000个字符")
    @Column(name = "key_verbs", columnDefinition = "TEXT")
    private String keyVerbs;

    /**
     * 是否系统预定义
     */
    @Column(name = "is_system")
    @Builder.Default
    private Boolean isSystem = true;

    /**
     * 使用次数
     */
    @Column(name = "usage_count")
    @Builder.Default
    private Long usageCount = 0L;

    /**
     * 状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", nullable = false, length = 20)
    @Builder.Default
    private EntityStatus status = EntityStatus.ACTIVE;

    /**
     * 创建时间
     */
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 创建人
     */
    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    /**
     * 更新人
     */
    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @PrePersist
    protected void onCreate() {
        LocalDateTime now = LocalDateTime.now();
        this.createdTime = now;
        this.updatedTime = now;
    }

    @PreUpdate
    protected void onUpdate() {
        this.updatedTime = LocalDateTime.now();
    }

    /**
     * 验证层级序号
     */
    public boolean validateHierarchyLevel() {
        return hierarchyLevel != null && hierarchyLevel >= 1 && hierarchyLevel <= 6;
    }

    /**
     * 判断是否为更高层次的认知能力
     */
    public boolean isHigherLevel(CognitiveLevel other) {
        if (other == null || other.hierarchyLevel == null || this.hierarchyLevel == null) {
            return false;
        }
        return this.hierarchyLevel > other.hierarchyLevel;
    }

    /**
     * 获取层次定义
     */
    public String getLevelDefinition() {
        if (description == null || description.trim().isEmpty()) {
            return levelName;
        }
        return levelName + "：" + description;
    }

    /**
     * 获取示例列表
     */
    public List<String> getExampleList() {
        if (exampleBehaviors == null || exampleBehaviors.trim().isEmpty()) {
            return List.of();
        }
        return Arrays.stream(exampleBehaviors.split("[,;，；]"))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
    }
}